package com.christodoulou.placesfinder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class BussinessFinder extends Activity {
	// All static variables
	
	//static final String URL = "http://10.0.2.2/Bussiness/bussiness.php?NAME=&BUSINESS_TYPE=&COUNTRY=";
  
	// XML node keys
	static final String KEY_BUSINESS = "business"; // parent node
	static final String KEY_ID = "ID";
	static final String KEY_NAME = "NAME";
	static final String KEY_TYPE = "BUSINESS_TYPE";
	static final String KEY_ADDRESS = "ADDRESS";
	static final String KEY_DISCRIPT = "DISCRIPT";
	static final String KEY_REGION = "REGION";
	static final String KEY_COUNTRY = "COUNTRY";
	static final String KEY_LATITUDE = "LATITUDE";
	static final String KEY_LONGITUDE = "LONGITUDE";
	static final String KEY_URL = "URL";
	
	ListView list;
    LazyAdapter adapter;
    ArrayList<HashMap<String, String>> businessList;
    @Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	    String URL = String.format(Locale.ENGLISH, "http://10.0.2.2/Bussiness/bussiness.php?NAME=&BUSINESS_TYPE=&COUNTRY=");

			
		// getting intent data
		Intent in = getIntent();
		
		Bundle b = in.getExtras();
	    if(b!=null)
	    {
	        String name =(String) b.get("name");
	        String type =(String) b.get("type");
	        String city =(String) b.get("city");
	        URL = String.format(Locale.ENGLISH, "http://10.0.2.2/Bussiness/bussiness.php?NAME=%s&BUSINESS_TYPE=%s&COUNTRY=%s",name,type,city);
	    }
	   
	   
		
		businessList = new ArrayList<HashMap<String, String>>();

		XMLParser parser = new XMLParser();
		String xml = parser.getXmlFromUrl(URL); // getting XML from URL
		Document doc = parser.getDomElement(xml); // getting DOM element
		
		NodeList nl = doc.getElementsByTagName(KEY_BUSINESS);
		// looping through all Bussiness nodes <root>
		for (int i = 0; i < nl.getLength(); i++) {
			// creating new HashMap
			HashMap<String, String> map = new HashMap<String, String>();
			Element e = (Element) nl.item(i);
			// adding each child node to HashMap key => value
			map.put(KEY_ID, parser.getValue(e, KEY_ID));
			map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
			map.put(KEY_TYPE, parser.getValue(e, KEY_TYPE));
			map.put(KEY_ADDRESS, parser.getValue(e, KEY_ADDRESS));
			map.put(KEY_DISCRIPT, parser.getValue(e, KEY_DISCRIPT));
			map.put(KEY_REGION, parser.getValue(e, KEY_REGION));
			map.put(KEY_COUNTRY, parser.getValue(e, KEY_COUNTRY));
			map.put(KEY_LATITUDE, parser.getValue(e, KEY_LATITUDE));
			map.put(KEY_LONGITUDE, parser.getValue(e, KEY_LONGITUDE));
			map.put(KEY_TYPE, parser.getValue(e, KEY_TYPE));			
			map.put(KEY_URL, parser.getValue(e, KEY_URL));

			// adding HashList to ArrayList
			businessList.add(map);
		}
		

		list=(ListView)findViewById(R.id.list);
		
		// Getting adapter by passing xml data ArrayList
        adapter=new LazyAdapter(this, businessList);        
        list.setAdapter(adapter);
        

        // Click event for single list row
 
        
            list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				
				
				HashMap<String, String> selectBusiness = businessList.get(position);
	             
				 // Starting new intent
	            //Intent intent = new Intent(getApplicationContext(), SingleListItem.class);
				//startActivity(intent);
				
				 Intent in = new Intent(getApplicationContext(), SingleListItem.class);
				 
	                in.putExtra(KEY_ID, selectBusiness.get(KEY_ID));
	                in.putExtra(KEY_NAME, selectBusiness.get(KEY_NAME));
	                in.putExtra(KEY_TYPE, selectBusiness.get(KEY_TYPE));
	                in.putExtra(KEY_ADDRESS, selectBusiness.get(KEY_ADDRESS));
	                in.putExtra(KEY_DISCRIPT, selectBusiness.get(KEY_DISCRIPT));
	                in.putExtra(KEY_REGION, selectBusiness.get(KEY_REGION));
	                in.putExtra(KEY_COUNTRY, selectBusiness.get(KEY_COUNTRY));
	                in.putExtra(KEY_LATITUDE, selectBusiness.get(KEY_LATITUDE));
	                in.putExtra(KEY_LONGITUDE, selectBusiness.get(KEY_LONGITUDE));  
	                in.putExtra(KEY_URL, selectBusiness.get(KEY_URL));	
	                startActivity(in);
				
							

			}
		});		
	}	
}